<?php defined('BASEPATH') or exit('No direct script access allowed');

/**
 *
 *
 * @package 	PyroCMS
 * @subpackage 	Lottery Model
 * @category 	Modules
 * @license 	Apache License v2.0
 */
class Transfer_m extends MY_Model {
	public function transfer($userId, $amount, $agentId, $affiliateId){

		$now=new DateTime();

		$this->db->insert('l_transaction', array(
        	'user_id' 				=> $userId,
        	'trans_type' 			=> 'TRANSFER',
        	'trans_date' 			=> $now->format(("Y-m-d H:i:s")),
        	'settle_date' 			=> $now->format('Y-m-d'),
        	'stake' 				=> $amount,
        	'win_lose' 				=> 0,
			'agent_id'				=> $agentId,
			'affiliate_id'			=> $affiliateId,
        	'status'	=> 'COMPLETED'
        	));
	}

	public function bonus($userId, $amount, $agentId, $affiliateId){

		$now=new DateTime();

		$this->db->insert('l_transaction', array(
        	'user_id' 				=> $userId,
        	'trans_type' 			=> 'BONUS',
        	'trans_date' 			=> $now->format(("Y-m-d H:i:s")),
        	'settle_date' 			=> $now->format('Y-m-d'),
        	'stake' 				=> $amount,
        	'win_lose' 				=> 0,
			'agent_id'				=> $agentId,
			'affiliate_id'			=> $affiliateId,
        	'status'	=> 'COMPLETED'
        	));
	}

	public function getTransferList($transType,$fromdate,$todate){

		$sql = "SELECT du.username, t.trans_type, ABS(t.win_lose-t.stake) AS amount, t.trans_date, t.status "
		." \nFROM l_transaction t INNER JOIN default_users du ON du.id = t.user_id"
		." \nWHERE date(t.settle_date) BETWEEN ? AND ? "
		." \nAND status ='COMPLETED' ";

		$params=array($fromdate->format("Y-m-d"),$todate->format("Y-m-d"));

		if($transType){
			$sql.=" \nAND trans_type = ?";
			$params[]=$transType;
		}

		$sql.=" \nORDER BY t.trans_date";

		return $this->db->query($sql, $params)->result();
	}
}